<template>
  <el-row class="mb-1">
    <el-button icon="plus" type="primary"  class="ml10" @click="addDate()">{{$t('operation.add')}}
    </el-button>
    <el-button :disabled="multiple" icon="Delete" type="danger" @click="handleDelete(selectObjs)">
      {{$t('btn.delete')}}
    </el-button>
    <el-button plain :disabled="multiple" icon="Download" type="success"  @click="downloadQrcode(selectObjs)">
      {{$t('qms.download')}}
    </el-button>
  </el-row>
  <el-table :data="state.dataList" v-loading="state.loading" border
            :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
            highlight-current-row
            @selection-change="selectionChangHandle"
            @sort-change="sortChangeHandle"
            @row-click="rowClick"
            style="width: 100%;height: 100%">
    <el-table-column type="selection" width="40" align="center"/>
    <el-table-column prop="title" :label="$t('qms.resourceName')" show-overflow-tooltip/>
    <el-table-column prop="categoryName" :label="$t('qms.classificationName')" show-overflow-tooltip/>
    <el-table-column prop="creator" :label="$t('sop.founder')" show-overflow-tooltip/>
    <el-table-column prop="createTime" :label="$t('qms.creationTime')" show-overflow-tooltip/>
    <el-table-column :label="$t('operation.Operation')" width="150">
      <template #default="scope">
        <el-button icon="edit" type="primary" @click="formDialogRef.openDialog(scope.row.uuid)">
        </el-button>
        <el-button icon="delete" type="danger" @click="handleDelete([scope.row.uuid])">
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
  <!-- 编辑、新增  -->
  <form-dialog ref="formDialogRef" @refresh="getDataList(false)"/>
</template>

<script setup lang="ts" name="systemArchive">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList, delObjs} from "/@/api/platform/mes/sysArchive";
import {useMessage, useMessageBox} from "/@/hooks/message";
import {useI18n} from "vue-i18n";
const {t} = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 父级点击的行
const parentClickRow = {}

//新增
const addDate = () =>{
  if(!parentClickRow.id){
    useMessage().error(t('sop.selectLeftType'));
    return
  }
  formDialogRef.value.openDialog(null,parentClickRow.id)
}

const emit = defineEmits<{ (e: 'clickHRow', value: any): void }>();


const rowClick = (value: any) =>{
  emit('clickHRow', value);
}

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 通过id进行子表的查询
const query = (row: Object) => {
  // 赋值查询参数
  state.queryForm.CategoryId= row.id
  // 保存选中的行数据
  Object.assign(parentClickRow, row)
  // 刷新子表数据
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/eletile-mes/sysArchive/export', Object.assign(state.queryForm, {ids: selectObjs}), 'sysArchive.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { uuid: string }[]) => {
  selectObjs.value = objs.map(({uuid}) => uuid);
  multiple.value = !objs.length;
};

//下载二维码
const downloadQrcode =(ids: string[])=>{
  downBlobFile('/eletile-mes/sysArchive/downloadQrCodeZip?showTitle=true&uuid=' + ids,null,"QcCode.zip")
}

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('operation.permanentDeletion'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('sop.successfullyDeleted'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};

// 暴露变量
defineExpose({
  query
});


</script>